﻿using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Windows;

namespace Pfz.WpfControls
{
	/// <summary>
	/// Gets the Children of a ModalPresenter as a read-only collection.
	/// </summary>
	public sealed class ModalPresenterChildren:
		IList<UIElement>
	{
		private List<ModalPresenterFrame> _children;
		internal ModalPresenterChildren(List<ModalPresenterFrame> children)
		{
			_children = children;
		}

		/// <summary>
		/// Gets the index of a given element.
		/// </summary>
		public int IndexOf(UIElement element)
		{
			int index = -1;
			foreach(var child in _children)
			{
				index++;

				if (child._element == element)
					return index;
			}

			return -1;
		}

		void IList<UIElement>.Insert(int index, UIElement element)
		{
			throw new ReadOnlyException();
		}

		void IList<UIElement>.RemoveAt(int element)
		{
			throw new ReadOnlyException();
		}

		/// <summary>
		/// Gets the element at the given index.
		/// </summary>
		public UIElement this[int index]
		{
			get
			{
				return _children[index]._element;
			}
		}

		UIElement IList<UIElement>.this[int index]
		{
			get
			{
				return this[index];
			}
			set
			{
				throw new ReadOnlyException();
			}
		}

		void ICollection<UIElement>.Add(UIElement item)
		{
			throw new ReadOnlyException();
		}

		void ICollection<UIElement>.Clear()
		{
			throw new ReadOnlyException();
		}

		/// <summary>
		/// Gets a value indicating if the given element is contained in this collection.
		/// </summary>
		/// <param name="element"></param>
		/// <returns></returns>
		public bool Contains(UIElement element)
		{
			return IndexOf(element) != -1;
		}

		/// <summary>
		/// Copies the items in this collection to the given array.
		/// </summary>
		public void CopyTo(UIElement[] array, int index)
		{
			foreach(var child in _children)
			{
				array[index] = child._element;
				index++;
			}
		}

		/// <summary>
		/// Gets the number of elements in this collection.
		/// </summary>
		public int Count
		{
			get
			{
				return _children.Count;
			}
		}

		bool ICollection<UIElement>.IsReadOnly
		{
			get
			{
				return true;
			}
		}

		bool ICollection<UIElement>.Remove(UIElement item)
		{
			throw new ReadOnlyException();
		}

		/// <summary>
		/// Gets an enumerator to iterate over all elements in this collection.
		/// </summary>
		public IEnumerator<UIElement> GetEnumerator()
		{
			foreach(var child in _children)
				yield return child._element;
		}

		#region IEnumerable Members
			IEnumerator IEnumerable.GetEnumerator()
			{
				return GetEnumerator();
			}
		#endregion
	}
}
